#!/bin/bash
# Copyright (c) 2024-2025 KoudaiPC Computer Technologies Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -e
set +e
echo -e "\n\033[32m\t*********Welcome to KPCOS!*********\033[0m\n"
echo -e "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++"

function check_shell_environment() {
  case $(uname -s) in 
    Linux)
          shell_result=$(/bin/sh -c 'echo ${BASH_VERSION}')
          if [ -n "${shell_result}" ]; then
            echo -e "\033[32mSystem shell: bash ${shell_result}\033[0m"
          else
            echo -e "\033[33m Your system shell isn't bash, we recommend you to use bash, because some commands may not be supported in other shells, such as pushd and shopt are not supported in dash. \033[0m"
          fi
          ;;
    Darwin)
          echo -e "\033[31m[KPCOS ERROR] Darwin system is not supported yet\033[0m"
          ;;
    *)
          echo -e "\033[31m[KPCOS ERROR] Unsupported this system: $(uname -s)\033[0m"
          exit 1
  esac
}

check_shell_environment 

echo -e "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo -e "\033[32mCurrent time: $(date +%F' '%H:%M:%S)\033[0m"
echo -e "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo -e "\033[32mBuild args: $@\033[0m"
echo -e "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++"

export BRANCH_TAG=KPC-OH-5.0.2-Release
export WORK_SPACE_DIR=$(cd $(dirname $0);pwd)

if [[ "${WORK_SPACE_DIR}x" == "x" ]]; then
  echo -e "\033[31m[KPCOS ERROR] WORK_SPACE_DIR cannot be empty.\033[0m"
  exit 1
fi

if [[ "${BRANCH_TAG}x" == "x" ]]; then
  echo -e "\033[31m[KPCOS ERROR] BRANCH_TAG cannot be empty.\033[0m"
  exit 1
fi

export PROJ_ROOT_DIR=$WORK_SPACE_DIR/$BRANCH_TAG
mkdir -p "${PROJ_ROOT_DIR}"

host_cpu_prefix=""

case $(uname -m) in
    *x86_64)
        host_cpu_prefix="x86"
        ;;
    *arm*)
        host_cpu_prefix="arm64"
        ;;
    *aarch*)
        host_cpu_prefix="arm64"
        ;;
    *)
        echo "\033[31m[KPCOS ERROR] Unsupported host arch: $(uname -m)\033[0m"
        RET=1
        exit $RET
esac

case $(uname -s) in
    Darwin)
        HOST_DIR="darwin-x86"
        HOST_OS="mac"
        ;;
    Linux)
        HOST_DIR="linux-x86"
        HOST_OS="linux"
        ;;
    *)
        echo "\033[31m[KPCOS ERROR] Unsupported host platform: $(uname -s)\033[0m"
        RET=1
        exit $RET
esac

function get_repo() {
  branch=$@
  echo -e "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
  echo -e "\033[32mInfo start get_repo: $(date +%F' '%H:%M:%S)\033[0m"
  echo -e "\033[32mInfo get_repo current dir: $(pwd)\033[0m"
  echo -e "\033[32mInfo get_repo branch: ${branch}\033[0m"
  echo -e "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
  repo init -u https://gitee.com/m8t/manifest.git -b $branch -g ohos:standard --no-repo-verify
  repo sync -c
  echo -e "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
  echo -e "\033[32mInfo start git lfs pull: $(date +%F' '%H:%M:%S)\033[0m"
  echo -e "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
  repo forall -c 'git lfs pull'
  echo -e "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
  echo -e "\033[32mInfo finish git lfs pull: $(date +%F' '%H:%M:%S)\033[0m"
  echo -e "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
}

pushd $PROJ_ROOT_DIR > /dev/null
  get_repo $BRANCH_TAG
popd > /dev/null


export PROJ_PATCH_DIR=$PROJ_ROOT_DIR/vendor/kpc/x86_general/patches

function patch_repo() {
  patched_stamp=./.patched_stamp
  echo -e "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
  echo -e "\033[32mInfo start patch_repo: $(date +%F' '%H:%M:%S)\033[0m"
  echo -e "\033[32mInfo patch_repo current dir: $(pwd)\033[0m"
  echo -e "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
  [ -e $patched_stamp ] && ./reverse-patch.sh && rm -f $patched_stamp
  ./patch.sh && touch $patched_stamp
}

pushd $PROJ_PATCH_DIR > /dev/null
  patch_repo
popd > /dev/null

echo -e "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
echo -e "\033[32mFinish time: $(date +%F' '%H:%M:%S)\033[0m"
echo -e "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++"
